/*
 *  Copyright 2010 BeTo.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package edu.upf.vod;

import edu.upf.core.ConfigDiscover;
import edu.upf.utils.LogSystem;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author BeTo
 */
public class VoDClient {

    private String vlcRoute, ip, port, vid, path, format;
    private LogSystem log;

    public VoDClient(String ip, String vid) throws IOException, InterruptedException {
        init();
        this.ip = ip;
        this.vid = vid;
        port = "5554";
        log = new LogSystem(this.getClass());
    }

    public void init() throws IOException, InterruptedException {
        ConfigDiscover conf = ConfigDiscover.getInstance();
        vlcRoute = conf.getVLCRoute();
    }

    public void setLocalparam(String path, String format) {
        this.path = path;
        this.format = format;
    }

    public void viewvideo(boolean local) throws IOException {
        String command;
        if (local) {
            command = vlcRoute + " " + path;
        } else {
            command = vlcRoute + " rtsp://" + ip + ":" + port + "/" + vid;
        }
        /*We create a new process executing the command needed to start up
        a new VoD VLC client*/
        final Process view = Runtime.getRuntime().exec(command);

        /*At this point we start 2 control threads to get the input and the
        errors to be shown*/
        Thread inputGetter = new Thread() {
            @Override
            public void run() {
                try {
                    InputStream is = view.getInputStream();
                    byte[] buffer = new byte[1024];
                    for (int count = 0; (count = is.read(buffer)) >= 0;) {
                        System.out.write(buffer, 0, count);
                    }
                } catch (IOException ex) {
                    log.addInfoLog(ex.getMessage());
                }

            }
        };
        inputGetter.start();

        Thread errorGetter = new Thread() {
            @Override
            public void run() {
                try {
                    InputStream is = view.getErrorStream();
                    byte[] buffer = new byte[1024];
                    for (int count = 0; (count = is.read(buffer)) >= 0;) {
                        System.err.write(buffer, 0, count);
                    }
                } catch (IOException ex) {
                    log.addInfoLog(ex.getMessage());
                }
            }
        };
        errorGetter.start();
    }
}

